package MD5

import (
	"crypto/md5"
	"encoding/hex"
	"io"
	"os"
)

func GetFileMD5Text(filePath string) (string, error) {
	data, err := GetFileMD5(filePath)
	if err != nil {
		panic(err)
	}
	return hex.EncodeToString(data), err
}

func GetFileMD5(filePath string) ([]byte, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	hash := md5.New()
	if _, err = io.Copy(hash, file); err != nil {
		return nil, err
	}

	return hash.Sum(nil), nil
}

func GetStringMD5(str string) string {
	hash := md5.Sum([]byte(str))

	return hex.EncodeToString(hash[:])
}
